<?php
/**
* 6W2100 全国批量代发代扣交易 代发代扣类
*/
class Conf6w2100 extends Abccbconf
{
    public function getTransaction()
    {
        $this->_ccbConf->getTransactionHeader();
        $this->_ccbConf->getTransactionSign();
    }

    public function getTransactionBody()
    {
        /**
         * 代发代扣编号 varChar(20) F 同网银代发代扣编号
         */
        $body['BILL_CODE'] = ($this->parameter['transferType'] == 1) ? $this->billCodeCcb : $this->billCode;

        /**
         * 代发代扣项目名称 varChar(100) 
         */
        $body['BILLDESC'] = ($this->parameter['transferType'] == 1) ? $this->billDescCcb : $this->billDesc;

        /**
         * Char(1) T 0 代发（空 默认代发） 1 代扣
         */
        $body['BILL_FLAG'] = isset($this->parameter['BILL_FLAG']) ? $this->parameter['BILL_FLAG'] : 0;;

        /**
         * 用途编号 varChar(12)
         */
        $body['USEOF_CODE'] = $this->useofCode;

        /**
         *  用途 varChar(100) 
         */
        $body['USE'] = $this->parameter['USE'];

        /**
         *  代发代扣文件名 varChar(50) F 文件上传到网银后返回的文件名
         */
        $body['SEND_FILE'] = $this->parameter['SEND_FILE'];

        /**
         *  原文件名 varChar(50) F 本地文件名 
         */
        $body['ORI_FILE'] = $this->parameter['ORI_FILE'];

        /**
         * 账号 varChar(32) T 代发时为付款账号，代扣时为收款账号 
         */
        $body['ACC_NO'] = $this->payAccNo;

        /**
         * 总金额 Decimal(16,2) T 建议填写该批上传文件总金额，避免填错文件
         */
        $body['AMOUNT'] = $this->parameter['AMOUNT'];

        /**
         *  总笔数 Decimal(7,0) T 建议填写该批上传文件总笔数，避免填错文件
         */
        $body['COUNT'] = $this->parameter['COUNT'];

        /**
         *  网银审批标识 Char(1) T 空或0.不需审批 1.网银审批
         */
        $body['FLOW_FLAG'] = isset($this->parameter['FLOW_FLAG']) ? $this->parameter['FLOW_FLAG'] : 0;

        /**
         *    异步处理标识 Char(1) T 0：正常处理 1：异步处理（异步处理总笔数必须大于5万） 
         */
        $body['ASYN_FLAG'] = isset($this->parameter['ASYN_FLAG']) ? $this->parameter['ASYN_FLAG'] : 0;

        /**
         * 返回文件解析后的多条错误 Char(1) T 1：一次返回批量文件中前100条错误(批量文件总笔数不能超出5000笔)0或空：按现有规则（有错误则跳出并返回错误码及错误描述）
         */
        $body['RET_ERRTYPE'] = isset($this->parameter['RET_ERRTYPE']) ? $this->parameter['RET_ERRTYPE'] : 0;

        return $body;
    }

    public function validateParameter()
    {
        if ( empty($this->parameter['SEND_FILE']) ) {
            throw new Exception("请输入代发代扣文件名", 1);
        }

        if ( empty($this->parameter['ORI_FILE']) ) {
            throw new Exception("请输入原文件名", 1);
        }

        if ( $this->parameter['AMOUNT'] <= 0 ) {
            throw new Exception("转账金额必须大于0", 1);
        }

        if ( $this->parameter['COUNT'] <= 0 ) {
            throw new Exception("请输入上传文件总笔数", 1);
        }
    }

    /**
     * 解析
     */
    public function parsesResult($response)
    {
        $transaction['message'] = (string)$response->RETURN_MSG;
        $transaction['request_sn'] = (string)$response->REQUEST_SN;
        $transaction['response_content'] = json_encode($response);
        if ( (string)$response->RETURN_CODE === '000000' ) { // 验证是否转账成功
            $transaction['SUCCESS_AMOUNT'] = $response->TX_INFO->SUCCESS_AMOUNT; // 交易总金额
            $transaction['SUCCESS_NUM']    = $response->TX_INFO->SUCCESS_NUM; //交易总笔数
            $transaction['status'] = 3; //交易成功
        } else {
            $transaction['status'] = 2; //交易失败
        }

        if ( ! empty($response->TX_INFO->Err_List->Err_Msg)) {
            foreach ($response->TX_INFO->Err_List->Err_Msg as $value) {
                $transaction['message'] = '<br\>'. $value;
            }
        }
        $transaction['state'] = 4;

        return $transaction;
    }
}